﻿#ifndef QGLOBALCONFIG_H
#define QGLOBALCONFIG_H

/***************************************
 * 【文件说明】： QT程序全局单例配置类
 * 【使用方法】：
    // 获取单例实例
    QGlobalConfig &config = QGlobalConfig::instance();

    // 写入不同类型的值
    config.write("General", "int8_t", (int8_t)127);
    config.write("General", "int16_t", (int16_t)-32768);
    config.write("General", "int32_t", (int32_t)2147483647);
    config.write("General", "int64_t", (int64_t)-9223372036854775808LL);
    config.write("General", "uint8_t", (uint8_t)255);
    config.write("General", "uint16_t", (uint16_t)65535);
    config.write("General", "uint32_t", (uint32_t)4294967295U);
    config.write("General", "uint64_t", (uint64_t)18446744073709551615ULL);
    config.write("General", "float", (float)3.14f);
    config.write("General", "double", (double)2.71828);
    config.write("General", "string", "Hello, World!");
    config.write("General", "QString", QString("你好，世界！"));

    // 写入容器类型
    QList<int> intList = {1, 2, 3, 4, 5};
    config.write("Containers", "QList<int>", QVariant::fromValue(intList));

    QVector<float> floatVector = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f};
    config.write("Containers", "QVector<float>", QVariant::fromValue(floatVector));

    QSet<QString> stringSet = {"apple", "banana", "cherry"};
    config.write("Containers", "QSet<QString>", QVariant::fromValue(stringSet));

    // 读取配置
    qDebug() << "int8_t:" << config.read("General", "int8_t").toInt();
    qDebug() << "int16_t:" << config.read("General", "int16_t").toInt();
    qDebug() << "int32_t:" << config.read("General", "int32_t").toInt();
    qDebug() << "int64_t:" << config.read("General", "int64_t").toLongLong();
    qDebug() << "uint8_t:" << config.read("General", "uint8_t").toUInt();
    qDebug() << "uint16_t:" << config.read("General", "uint16_t").toUInt();
    qDebug() << "uint32_t:" << config.read("General", "uint32_t").toULongLong();
    qDebug() << "uint64_t:" << config.read("General", "uint64_t").toULongLong();
    qDebug() << "float:" << config.read("General", "float").toFloat();
    qDebug() << "double:" << config.read("General", "double").toDouble();
    qDebug() << "string:" << config.read("General", "string").toString();
    qDebug() << "QString:" << config.read("General", "QString").toString();

    // 读取容器类型
    qDebug() << "QList<int>:" << QVariant(config.read("Containers", "QList<int>")).toList();
    qDebug() << "QVector<float>:" << QVariant(config.read("Containers", "QVector<float>")).toList();
    qDebug() << "QSet<QString>:" << QVariant(config.read("Containers", "QSet<QString>")).toList();

    // 移除指定键
    config.remove("General", "int8_t");

    // 清空所有配置
    config.clear();
***************************************/

#include <QSettings>
#include <QVariant>
#include <QMutex>
#include <QString>
#include <cstdint>
#include <memory>
#include <QReadWriteLock>

class QGlobalConfig
{
public:
    // 获取单例实例
    static QGlobalConfig& getInstance();
    // 删除单例
    static void deleteInstance();
    //初始化
    void init(QString configPath,QMap<QString,QVariant> defaultSettings = QMap<QString,QVariant>());
    //反初始化-默认无需调用。deleteInstance时自动调用
    void uninit();
    // 写入配置
    void write(const QString &group, const QString &key, const QVariant &value);

    // 写入配置
    void write(const QString &key, const QVariant &value);

    // 读取配置
    QVariant read(const QString &group, const QString &key, const QVariant &defaultValue = QVariant());

    // 读取配置
    QVariant read(const QString &key, const QVariant &defaultValue = QVariant());

    // 移除指定键
    void remove(const QString &group, const QString &key);

    // 移除指定键
    void remove(const QString &key);

    // 清空所有配置
    void clear();

    //提交配置,直接保存一次
    void push();

private:
    QGlobalConfig();
    ~QGlobalConfig();

    bool isInit;

    static QGlobalConfig* instance;
    static QMutex mutex;

    QMap<QString,QVariant> settingsMap;

    QSettings *settings;
    mutable QReadWriteLock m_lock;  // 用于保护配置文件的读写操作
};
#endif // QGLOBALCONFIG_H
